package com.sq.util.exception;

import com.sq.util.StringUtil;

/**
 * 不要打印log和堆栈，要返回错误信息到前端
 * 
 * @author sq
 * 
 */
public class NoLogException extends RuntimeException {

    /**
     * 
     */
    private static final long serialVersionUID = -4685700624782364996L;

    public NoLogException() {
    }

    public NoLogException(String msg) {
        super(msg);
    }

    public NoLogException(String format, Object... args) {
        super(StringUtil.format(format, args));
    }

    public NoLogException(Throwable t, String msg) {
        super(msg, t);
    }

    public NoLogException(Throwable t, String format, Object... args) {
        super(StringUtil.format(format, args), t);
    }

    /**
     * 
     * @param format
     *            slf4j那样,{}替换为args
     * @param args
     */
    public static void isTrue(boolean bool, String format, Object... args) {
        if (!bool) {
            throw getException(format, args);
        }
    }

    /**
     * 
     * @param format
     *            slf4j那样,{}替换为args
     * @param args
     */
    public static void assertNotBlank(String str, String format, Object... args) {
        if (StringUtil.isBlank(str)) {
            throw getException(format, args);
        }
    }

    /**
     * 
     * @param format
     *            slf4j那样,{}替换为args
     * @param args
     */
    public static void assertBlank(String str, String format, Object... args) {
        if (StringUtil.isNotBlank(str)) {
            throw getException(format, args);
        }
    }

    /**
     * 
     * @param format
     *            slf4j那样,{}替换为args
     * @param args
     */
    public static void assertNotNull(Object obj, String format, Object... args) {
        if (obj == null) {
            throw getException(format, args);
        }
    }

    /**
     * 
     * @param format
     *            slf4j那样,{}替换为args
     * @param args
     */
    public static void assertNull(Object obj, String format, Object... args) {
        if (obj != null) {
            throw getException(format, args);
        }
    }

    private static NoLogException getException(String format, Object... args) {
        return new NoLogException(StringUtil.format(format, args));
    }
}
